package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class LoginFrame extends JFrame {
    private JTextField userIdField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setTitle("实验平台系统 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        getContentPane().setBackground(new Color(245, 245, 245));

        // 标题标签
        JLabel titleLabel = new JLabel("实验平台系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setBounds(0, 20, 400, 40);
        add(titleLabel);

        // 用户ID
        JLabel idLabel = new JLabel("用户ID:");
        idLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        idLabel.setBounds(60, 80, 80, 25);
        add(idLabel);

        userIdField = new JTextField();
        userIdField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        userIdField.setBounds(140, 80, 200, 32);
        add(userIdField);

        // 密码
        JLabel passLabel = new JLabel("密码:");
        passLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        passLabel.setBounds(60, 130, 80, 25);
        add(passLabel);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField.setBounds(140, 130, 200, 32);
        add(passwordField);

        // 登录按钮
        JButton loginButton = new JButton("登 录");
        loginButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        loginButton.setBackground(Color.WHITE);
        loginButton.setForeground(Color.BLACK);
        loginButton.setFocusPainted(false);
        loginButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 添加手型光标
        loginButton.setBounds(60, 190, 120, 40);
        loginButton.addActionListener(this::performLogin);
        add(loginButton);

        // 注册按钮
        JButton registerButton = new JButton("注 册");
        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        registerButton.setBackground(new Color(100, 149, 237));
        registerButton.setForeground(Color.BLACK);
         registerButton.setFocusPainted(false);
        registerButton.setBounds(220, 190, 120, 40);
        registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 添加手型光标
        registerButton.addActionListener(this::showRegisterFrame);
        add(registerButton);

        // 支持回车键登录
        getRootPane().setDefaultButton(loginButton);
    }

    private void performLogin(ActionEvent e) {
        String userId = userIdField.getText();
        String password = new String(passwordField.getPassword());
        User user = AuthService.authenticate(userId, password);

        if (user != null) {
            new MainFrame(user).setVisible(true);
            dispose(); // 关闭登录窗口
        } else {
            JOptionPane.showMessageDialog(this,
                    "用户名或密码错误",
                    "登录失败",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showRegisterFrame(ActionEvent e) {
        new RegisterFrame().setVisible(true);
    }
}